package com.qlk.odcinterface.manager.impl;

import com.qlk.common.manager.BaseManager;
import com.qlk.common.utils.page.PageUtil;
import com.qlk.odcinterface.dao.PmsFullCutChannelDao;
import com.qlk.odcinterface.dao.PmsFullCutDao;
import com.qlk.odcinterface.dao.PmsFullCutItemDao;
import com.qlk.odcinterface.domain.PmsFullCut;
import com.qlk.odcinterface.domain.PmsFullCutChannel;
import com.qlk.odcinterface.domain.PmsFullCutItem;
import com.qlk.odcinterface.domain.query.PmsFullCutQuery;
import com.qlk.odcinterface.manager.PmsFullCutManager;
import com.qlk.odcinterface.sdk.api.request.PmsFullCutRequest;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * PmsFullCutManager接口的实现类
 * 
 * @author zhaoyanan
 * @version 2016-4-14 17:10:21
 * 
 */
@Component
public class PmsFullCutManagerImpl extends BaseManager implements PmsFullCutManager {
	
    @Autowired
    private PmsFullCutDao pmsFullCutDao;

    @Autowired
    private PmsFullCutItemDao pmsFullCutItemDao;

    @Autowired
    private PmsFullCutChannelDao pmsFullCutChannelDao;

    /**
     * {@inheritDoc}
     */
    public boolean insert(final List<PmsFullCut> pmsFullCutList) {
        boolean resultFlag = false;
        if (null != pmsFullCutList && pmsFullCutList.size() > 0) {
            for (PmsFullCut pmsFullCut : pmsFullCutList) {
                resultFlag = pmsFullCutDao.insert(pmsFullCut);
                if (!resultFlag) {
                    throw new RuntimeException("批量新增表信息异常");
                }
            }
        }

        return resultFlag;
    }

    /**
     * {@inheritDoc}
     */
    public boolean insert(PmsFullCut pmsFullCut) {
        return pmsFullCutDao.insert(pmsFullCut);
    }

    /**
     * {@inheritDoc}
     */
    public boolean update(final PmsFullCut pmsFullCut) {
        return pmsFullCutDao.update(pmsFullCut);
    }

    /**
     * {@inheritDoc}
     */
    public List<PmsFullCut> queryPmsFullCutList(PmsFullCutQuery queryBean) {
        return pmsFullCutDao.queryPmsFullCutList(queryBean);
    }

    /**
     * {@inheritDoc}
     */
    public List<PmsFullCut> queryPmsFullCutListWithPage(PmsFullCutQuery queryBean,
            PageUtil pageUtil) {
        if (null == queryBean) {
            queryBean = new PmsFullCutQuery();
        }

        // 查询总数
        int totalItem = queryPmsFullCutCount(queryBean);

        if (pageUtil == null) {
            pageUtil = new PageUtil();
        }
        pageUtil.setTotalRow(totalItem);
        pageUtil.init();
        
        if (totalItem > 0) {
            queryBean.setPageIndex(pageUtil.getCurPage());
            queryBean.setPageSize(pageUtil.getPageSize());
            // 调用Dao翻页方法
            return pmsFullCutDao.queryPmsFullCutListWithPage(queryBean);
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public int queryPmsFullCutCount(PmsFullCutQuery queryBean) {
        return pmsFullCutDao.queryPmsFullCutCount(queryBean);
    }

    /**
     * {@inheritDoc}
     */
    public boolean delete(PmsFullCut pmsFullCut) {
        return pmsFullCutDao.delete(pmsFullCut);
    }

    /**
     * {@inheritDoc}
     */
    public PmsFullCut getPmsFullCutById(Long id) {
        return pmsFullCutDao.getPmsFullCutById(id);
    }

    /**
     * {@inheritDoc}
     */
    public boolean delete(final PmsFullCut[] pmsFullCuts) {
        boolean resultFlag = false;
        if (null != pmsFullCuts && pmsFullCuts.length > 0) {
            for (int i = 0; i < pmsFullCuts.length; i++) {
                resultFlag = delete(pmsFullCuts[i]);
                if (!resultFlag) {
                    throw new RuntimeException("批量删除表信息异常!");
                }
            }
        }

        return resultFlag;
    }

    /**
     * {@inheritDoc}
     */
    public boolean exist(PmsFullCut pmsFullCut) {
        return pmsFullCutDao.exist(pmsFullCut);
    }

    public boolean syncFullCutInfo(PmsFullCutRequest fullCutRequest, String isStatusUpdate) {

        PmsFullCut param;
        //说明只更新状态
        if ("1".equals(isStatusUpdate)){
            param = new PmsFullCut();
            param.setStatus(fullCutRequest.getStatus());
            param.setId(fullCutRequest.getId());
            pmsFullCutDao.update(param);
            return true;
        }

        param = new PmsFullCut();
        //复制属性
        BeanUtils.copyProperties(fullCutRequest, param);
        //全表更新
        if ("0".equals(isStatusUpdate)){
//            更新主表信息
            pmsFullCutDao.update(param);
//            删除字表信息
            PmsFullCutItem pmsFullCutItem = new PmsFullCutItem();
            pmsFullCutItem.setPromotionId(fullCutRequest.getId());
            pmsFullCutItemDao.delete(pmsFullCutItem);
        }
        //新增操作
        if (StringUtils.isEmpty(isStatusUpdate)){
//            插入主表信息
            pmsFullCutDao.insert(param);
        }
        //字表插入集合
        List<PmsFullCutItem> insetItemList = fullCutRequest.getItemList();
        batchInsertItem(insetItemList);
        //批量添加渠道
        List<PmsFullCutChannel> channelList = fullCutRequest.getChannelList();
        batchInsertChannel(channelList);
        return true;
    }

    /**
     * 批量新增字表数据
     * @param insetItemList
     */
    private void batchInsertItem(List<PmsFullCutItem> insetItemList){
        if (CollectionUtils.isNotEmpty(insetItemList)){
            for (PmsFullCutItem item : insetItemList){
                pmsFullCutItemDao.insert(item);
            }
        }
    }

    /**
     * 批量新增渠道
     * @param channelList
     */
    private void batchInsertChannel(List<PmsFullCutChannel> channelList){
        if (CollectionUtils.isNotEmpty(channelList)){
            for (PmsFullCutChannel item : channelList){
                pmsFullCutChannelDao.insert(item);
            }
        }
    }
}
